package org.fzu.ercha.wuta.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import org.fzu.ercha.wuta.Entity.Audio;
import org.fzu.ercha.wuta.MyApplication;
import org.fzu.ercha.wuta.R;

import java.util.List;

/**
 * Created by ercha on 2017/1/26.
 */

public class SongAdapter extends ArrayAdapter<Audio> {
    private Context mContext = null;
    private int resourceId;

    private SongAdapter self = null;

    private OnSongListChangedListener mSongListChangedListener = null;

    public SongAdapter(Context context, int textViewResourceId, List<Audio> paidOrderItems) {
        super(context, textViewResourceId, paidOrderItems);
        this.resourceId = textViewResourceId;
        mContext = context;
        self = this;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Audio item = getItem(position);
        View view;
        ViewHolder viewHolder;

        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();

            viewHolder.title = (TextView)view.findViewById(R.id.title);
            viewHolder.artist = (TextView)view.findViewById(R.id.artist);
            viewHolder.album = (TextView)view.findViewById(R.id.album);
            viewHolder.deleteBtn = (Button)view.findViewById(R.id.deleteBtn);
            viewHolder.deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(getContext())
                            .setTitle(item.getTitle())
                            .setMessage("是否从列表中移除这首歌?")
                            .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    MyApplication.removeAudioById(item.getId());
                                    self.notifyDataSetChanged();
                                    if(mSongListChangedListener != null){
                                        mSongListChangedListener.onSongDeleted();
                                    }
                                }
                            })
                            .setNegativeButton("否", null).show();
                }
            });

            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }

        viewHolder.title.setText(item.getTitle());
        viewHolder.artist.setText(item.getArtist());
        viewHolder.album.setText(item.getAlbum());

        return view;
    }

    class ViewHolder{
        TextView title;
        TextView artist;
        TextView album;
        Button deleteBtn;
    }

    public void setOnSongListChangedListener(OnSongListChangedListener listener){
        mSongListChangedListener = listener;
    }

    public interface OnSongListChangedListener{
        void onSongDeleted();
    }

}
